package com.fantasy.easy.biz.utils;

import com.fantasy.easy.biz.domain.dto.ImageInfoDTO;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import static com.fantasy.easy.biz.domain.dto.ImageInfoDTO.createImageDTO;

/**
 * @Description TODO
 * @Author sdehua-帅德华
 * @Email sdhstart@qq.com
 * @Date 2022/6/27 11:06
 * @Version 1.0
 **/
public class ImageMarkUtils {
    /**
     * 编辑图片,往指定位置添加文字
     *
     * @param srcImgPath    :源图片路径
     * @param targetImgPath :保存图片路径
     * @param list          :文字集合
     */
    public static Boolean writeImage(String srcImgPath, String targetImgPath, List<ImageInfoDTO> list) {
        FileOutputStream outImgStream = null;
        String fileExt = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1);
        try {
            //读取原图片信息
            //得到文件
            File srcImgFile = new File(srcImgPath);
            //文件转化为图片
            Image srcImg = ImageIO.read(srcImgFile);
            //获取图片的宽
            int srcImgWidth = srcImg.getWidth(null);
            //获取图片的高
            int srcImgHeight = srcImg.getHeight(null);

            //添加文字:
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            for (ImageInfoDTO imgDTO : list) {
                //根据图片的背景设置水印颜色
                g.setColor(imgDTO.getColor());
                g.setFont(imgDTO.getFont());
                //防锯齿
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                //设置字体
                int x = imgDTO.getX();
                //判断是否居中
                if (imgDTO.getIsCenter()) {
                    FontMetrics fm = g.getFontMetrics(imgDTO.getFont());
                    x = (srcImgWidth - fm.stringWidth(imgDTO.getText())) / 2;
                }
                //画出水印
                g.drawString(imgDTO.getText(), x, imgDTO.getY());
            }
            g.dispose();
            // 输出图片
            outImgStream = new FileOutputStream(targetImgPath);
            return ImageIO.write(bufImg, fileExt, outImgStream);
        } catch (Exception e) {
            System.out.println("系统异常");
            return false;
        } finally {
            try {
                if (null != outImgStream) {
                    outImgStream.flush();
                    outImgStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void createCertifyImage(String name,String NO,String date,String level){
        //模板文件地址
//        String srcImgPath = "D:\\MyTest\\certify-raw.png";
        String srcImgPath = System.getProperty("user.dir")+"/easy-sys/src/main/resources/img/certify-raw.png";
        //证书生成地址
        String tarImgPath = System.getProperty("user.dir")+"/easy-sys/src/main/resources/img/certify-new.png";
        // 证书所有者姓名
//        String name = "张三";
        // 编号
//        String NO = "BANK-SYSTEM-"+123456;
        NO = "BANK-SYSTEM-"+NO;
        // 日期
//        String date = "2022-06-27";
        // 资信等级
//        String level = "★★★★★";
        int l = Integer.parseInt(level);
        if (80 <= l){
            level = "★★★★★";
        } else if (60 <= l){
            level = "★★★★";
        } else if (40 <= l){
            level = "★★★";
        } else if (20 <= l){
            level = "★★";
        } else {
            level = "★";
        }
        // 套话
        String prize1 = "依据GB/T23794-2015《个人信用评价标准》,并结合该系统信用报告，";
        String prize2 = "对其个人信用状况进行客观、科学、公正的评估，确定其资质等级为:";
        //创建图片文字数据集合；
        List<ImageInfoDTO> list = new ArrayList<>();
        list.add(createImageDTO(name, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 100), 500, 700, true));
        list.add(createImageDTO(prize1, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 50), 450, 800, true));
        list.add(createImageDTO(prize2, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 50), 450, 950, true));
        list.add(createImageDTO(level, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 100), 450, 1100, true));
        list.add(createImageDTO("证书编号:"+NO, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 36), 250, 1290, false));
        list.add(createImageDTO(date, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 36), 1800, 1520, false));
        //操作图片:
        ImageMarkUtils.writeImage(srcImgPath, tarImgPath, list);
    }

    public static void createCreditImage(String name,String NO,String date,Integer level){
        String srcImgPath = System.getProperty("user.dir")+"/easy-sys/src/main/resources/img/certify-raw.png";
        String tarImgPath = System.getProperty("user.dir")+"/easy-sys/src/main/resources/img/certify-new.png";
        NO = "BANK-SYSTEM-"+NO;
        String prize1 = "依据GB/T23794-2015《个人信用评价标准》,并结合该系统信用报告，";
        String prize2 = "对其个人信用状况进行客观、科学、公正的评估，确定其信用水平分数为:";
        //创建图片文字数据集合；
        List<ImageInfoDTO> list = new ArrayList<>();
        list.add(createImageDTO(name, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 100), 500, 700, true));
        list.add(createImageDTO(prize1, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 50), 450, 800, true));
        list.add(createImageDTO(prize2, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 50), 450, 950, true));
        list.add(createImageDTO(level.toString(), Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 200), 450, 1200, true));
        list.add(createImageDTO("证书编号:"+NO, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 36), 250, 1290, false));
        list.add(createImageDTO(date, Color.BLACK, FontUtil.loadFontResource(Font.BOLD, 36), 1800, 1520, false));
        //操作图片:
        ImageMarkUtils.writeImage(srcImgPath, tarImgPath, list);
    }
//    public static void main(String[] args) {
//        ImageMarkUtils.createCertifyImage("李四","123987","2022/12/15","78");
//    }
}
